package basic.study.wantOffer.chapter3;


/**
 * @author Kevin
 * @date 2020/5/3 12:39
 * 链表删除重复的节点
 */
public class Problem18_ {
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode pre = null;
        ListNode cur = pHead;
        while (cur != null) {
            // 出现重复
            if (cur.next != null && cur.val == cur.next.val) {
                while (cur.next != null && cur.next.val == cur.val) {
                    cur = cur.next;
                }
                cur = cur.next;
                if (pre == null) {
                    pHead = cur;
                } else {
                    pre.next = cur;
                }
            } else {// 无重复
                pre = cur;
                cur = cur.next;
            }
        }
        return pHead;
    }

    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int c) {
            this.val = c;
        }
    }
}
